package br.odb.libscene;

import br.odb.utils.Utils;

public class Color {

	public Color( long argb) {

		a = (int) ( ( argb & 0xFF000000 ) >> 24  );
		r = (int) ( ( argb & 0x00FF0000 ) >> 16 );
		g = (int) ( ( argb & 0x0000FF00 ) >> 8  );
		b = (int) ( ( argb & 0x000000FF )       );
	}

	// ------------------------------------------------------------------------------------------------------------
	
	public Color( int r, int g, int b, int a ) {
		this.setR( r);
		this.setG( g);
		this.setB( b);
		this.setA( a);
	}
	
	public Color() {
		this( 0,0,0,255 );
	}
	
	
	public Color(int r, int g, int b) {
		this( r, g, b, 255 );
	}

	public Color(Color c) {
		this( c.r, c.g, c.b, c.a );
	}

	public Color(float r, float g, float b ) {
		this.r = (int) (r * 255);
		this.g = (int) (g * 255);
		this.b = (int) (b * 255);
		this.a = 255;
	}

	public Color(float r2, float g2, float b2, float a2) {
		this( r2, g2, b2 );
		a = (int) ( 255 * a2 );
	}

	/**
	 * @param r the r to set
	 */
	public void setR( int r) {
		this.r = Utils.clamp( r, 0, 255 );
//		System.out.println( " r = " + r );
	}
	/**
	 * @return the r
	 */
	public int getR() {
		return r;
	}

	/**
	 * @param g the g to set
	 */
	public void setG( int g) {
		this.g = Utils.clamp( g, 0, 255 );
//		System.out.println( " g = " + g );
	}

	/**
	 * @return the g
	 */
	public int getG() {
		return g;
	}

	/**
	 * @param b the b to set
	 */
	public void setB( int b) {
		this.b = Utils.clamp( b, 0, 255 );
//		System.out.println( " b = " + b );
	}

	/**
	 * @return the b
	 */
	public int getB() {
		return b;
	}

	/**
	 * @param a the a to set
	 */
	public void setA( int  a) {
		this.a = ( int ) Utils.clamp( a, 0, 255 );
//		System.out.println( " a = " + a );
	}

	/**
	 * @return the a
	 */
	public int getA() {
		return a;
	}
	
	public String toString() {
		String toReturn = "";
		String hex;

//		hex = Integer.toHexString( r );		
//		if ( hex.length() == 1 )
//			hex = "0" + hex;
		
		hex = Integer.toString( r );		
		toReturn += hex + " ";
		
//		hex = Integer.toHexString( g );		
//		if ( hex.length() == 1 )
//			hex = "0" + hex + " ";

		hex = Integer.toString( g );		
		toReturn += hex + " ";
		
//		hex = Integer.toHexString( b );		
//		if ( hex.length() == 1 )
//			hex = "0" + hex;

		hex = Integer.toString( b );		
		toReturn += hex + " ";

		hex = Integer.toString( a );		
		toReturn += hex;

		return toReturn;
	}

	/**
	 * 	
	 */
	public int hashCode(){	
		return toString().hashCode();
	}
	
	
	/**
	 * 
	 */
	public boolean equals( Object that ) {
		if (this == that)
			return true;
		if ( !(that instanceof Color ) )
			return false;
		return this.hashCode() == that.hashCode();
	}
	
	private int r;
	private int g;
	private int b;
	private int a;
	
	public static long getARGBColor(Color color) {
		return color.getARGBColor();
	}

	
	
	public long getARGBColor() {
		
		if ( a >= 255)
			 a = 255;
		
		if (r >= 255)
			r = 255;
		
		if (g >= 255)
			g = 255;
		
		if (b >= 255)
			b = 255;

		if ( a < 0)
			 a = 0;
		
		if (r < 0)
			r = 0;
		
		if (g < 0)
			g = 0;
		
		if (b < 0)
			b = 0;
		
		long color;
		
		color = ( long )( a * 0x1000000 );
		color += r * 0x10000;
		color += g * 0x100;
		color += b;
		
		return color;

	}

	public void set( int r, int g, int b, int a ) {		
		setA( a );
		setR( r );
		setG( g );
		setB( b );		
	}

	public void set( int r, int g, int b ) {		
		set( r, g, b, 255 );
	}

	
	public static Color getColorFromHTMLColor( String htmlColor ) {
		Color toReturn = new Color();
		
		toReturn.setR( Integer.parseInt( htmlColor.substring( 1, 3 ), 16 ) );
		toReturn.setG( Integer.parseInt( htmlColor.substring( 3, 5 ), 16 ) );
		toReturn.setB( Integer.parseInt( htmlColor.substring( 5, 7 ), 16 ) );
		toReturn.setA( 255 );
		
		return toReturn;
	}
	
	public String getHTMLColor() {
		String rHex =  Integer.toString( ( r & 0xff ) + 0x100, 16 ).substring( 1 );
		String gHex =  Integer.toString( ( g & 0xff ) + 0x100, 16 ).substring( 1 );
		String bHex =  Integer.toString( ( b & 0xff ) + 0x100, 16 ).substring( 1 );
		return "#" + rHex + gHex + bHex;
	}

	public void copyFrom(Color color) {
		
		this.a = color.a;
		this.r = color.r;
		this.g = color.g;
		this.b = color.b;
	}

	public String getExplicitRGBColor() {

		return "rgb(" + r + "," + g + ", " + b + ")";
	}
}
